(function() {
	"use strict";
	var SliderPic = function(ele, options) {
		this.ele = ele;
		this.defaults = {
			Title: "",
			width: "300px",
			topWin: (function(p, c) {
				while(p != c) {
					c = p;
					p = p.parent;
				}
				return c;
			})(window.parent, window),
			data: []
		}
		this.options = $.extend(this.defaults, options);
	};
	SliderPic.prototype = {
		initData: function() {
			let _This = this;
			let _args = _This.defaults;
			let _ThisEle = _This.ele;
			let itemdata = _args.data;
			if(!Array.isArray(itemdata) || itemdata.length <= 0) {
				return;
			}
			let _sliderContent = $("<div class='sliderContent'></div>");
			let itemimgs = this.loadItem(itemdata);
			_sliderContent.append(itemimgs);
			let arrayLength = itemdata.length;
			let itemnum = this.loadNum(arrayLength);
			_sliderContent.append(itemnum);
			_ThisEle.append(_sliderContent);
			return(function() {
				let time = "";
				let index = 1;
				let _itemSpan=_sliderContent.find(".slideritemnum span");
				let _itemLi=_sliderContent.find("ul.slideritemsimg li");				
				_itemSpan.hover(function() {
					index=$(this).index();
					clearTimeout(time);
					_itemSpan.removeClass("selected").eq(index).addClass("selected");
					_itemLi.fadeOut(2000).stop(true, true).eq(index).fadeIn(1000);
				}, function() {
					index = ++index%arrayLength;
					time = setInterval(function() {
						showimg(index);
					}, 3000)
				});
				time = setInterval(function() {
					showimg(index);
				}, 3000);
				function showimg(num) {
					_itemSpan.removeClass("selected").eq(index).addClass("selected");
					_itemLi.fadeOut(2000).stop(true, true).eq(index).fadeIn(1000);
					index = ++index%arrayLength;
				}
			})();
		},
		loadItem: function(itemdata) {
			var _args = this.defaults;
			var _sliderContent = $("<div class='sliderContent'></div>");
			var _itemsimg = $("<ul class='slideritemsimg'></ul>");
			var _imgItem = "<li><a href='{0}'><img class='sliderimg' src='{1}' /></a></li>";
			let allitem = "";
			itemdata.forEach(function(item, index) {
				let itemImg = _imgItem;
				let aurl = "javascript:;";
				let imgurl = "";
				if(item.hasOwnProperty("aurl") && item["aurl"] != "") {
					aurl = item["aurl"];
				}
				if(item.hasOwnProperty("imgurl")) {
					imgurl = item["imgurl"];
				}
				itemImg = itemImg.replace(/\{0}/, aurl).replace(/\{1}/, imgurl);
				allitem += itemImg;
			});
			_itemsimg.html(allitem);
			return _itemsimg;
		},
		loadNum: function(count) {
			let _itemnum = $("<div class='slideritemnum'></div>");
			let nums = "<span class='selected'>1</span>";
			if(count > 1) {
				let numone = "<span>{0}</span>";
				for(let i = 2; i <= count; i++) {
					let item = numone;
					nums += item.replace("{0}", i);
				}
			}
			return _itemnum.html(nums);
		}

	};
	$.fn.Slider = function(options) {
		var slider = new SliderPic($(this), options);
		return slider.initData();
	};
})(jQuery);